﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;

namespace UMLDesigner
{
    class ClassView
    {
        private TableLayoutPanel panel;
        private Label labelName;
        private ListBox listBoxAttributes;
        private ListBox listBoxMethods;
        private bool dragging = false;

        private ClassData classData = new ClassData();

        public ClassView(int x, int y)
        {
            panel = new TableLayoutPanel();
            panel.BackColor = System.Drawing.Color.White;
            panel.AutoSize = true;
            panel.Size = new Size(0, 0);
            panel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            panel.Location = new Point(x, y);

            labelName = new Label();
            labelName.BackColor = System.Drawing.Color.Silver;            
            //labelName.Font = new Font(panel.Font, FontStyle.Bold);
            labelName.Margin = new Padding(0);
            labelName.Padding = new Padding(5);
            labelName.AutoSize = true;
            labelName.Dock = DockStyle.Fill;
            labelName.Cursor = Cursors.Hand;
            labelName.TextAlign = ContentAlignment.MiddleCenter;
            panel.Controls.Add(labelName, 0, 0);

            listBoxAttributes = new ListBox();
            for (int i = 0; i < classData.GetAttributes().Count; i++)
            {
                listBoxAttributes.Items.Add(classData.GetAttributes()[i].GetName());
            }
            listBoxAttributes.Dock = DockStyle.Fill;
            listBoxAttributes.BackColor = panel.BackColor;
            listBoxAttributes.AutoSize = true;
            listBoxAttributes.SelectionMode = SelectionMode.None;
            listBoxAttributes.BorderStyle = BorderStyle.None;
            panel.Controls.Add(listBoxAttributes, 0, 1);

            listBoxMethods = new ListBox();
            listBoxMethods.Dock = DockStyle.Fill;
            listBoxMethods.BackColor = panel.BackColor;
            listBoxMethods.AutoSize = true;
            listBoxMethods.SelectionMode = SelectionMode.None;
            
            listBoxMethods.BorderStyle = BorderStyle.None;
            panel.Controls.Add(listBoxMethods, 0, 2);

            AddEventHandlers(panel);

            //Change the data and call refresh
            classData.SetName("ClassWithoutName");
            classData.SetVisibility(VisibilityType.PUBLIC);
            classData.SetModifier(ModifiersTypes.NONE);

            Refresh();
        }

        private void Clear()
        {
            listBoxMethods.Items.Clear();
            listBoxAttributes.Items.Clear();
        }

        public void Refresh()
        {
            string modifier = "";
            if (classData.GetModifier() != ModifiersTypes.NONE)
            {
                modifier = ModifiersTypes.GetUMLRepresentation(classData.GetModifier()) + "\n";
            }

            string visibility = VisibilityType.GetUMLRepresentation(classData.GetVisibility());
            labelName.Text = modifier + visibility + " " + classData.GetName();

            listBoxAttributes.Items.Clear();
            for (int i = 0; i < classData.GetAttributes().Count; i++)
            {
                listBoxAttributes.Items.Add(classData.GetAttributes()[i].GetUMLRepresentation());
            }

            //fit minimum size 
            panel.Size = new Size(0, 0);
        }

        private void AddEventHandlers(Control c)
        {
            c.MouseDown += new MouseEventHandler(OnMouseDownEventHandler);
            c.MouseUp += new MouseEventHandler(OnMouseUpEventHandler);
            c.MouseMove += new MouseEventHandler(OnMouseMoveEventHandler);
            
            foreach(Control child in c.Controls) {
                AddEventHandlers(child);
            }
        }

        private void OnMouseUpEventHandler(object sender, MouseEventArgs e)
        {
            dragging = false;
        }

        private void OnMouseMoveEventHandler(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                Point p = panel.Location + new Size(e.Location);
                p.X = p.X - panel.Width/2;
                panel.Location = p;
            }
        }

        private void OnMouseDownEventHandler(object sender, MouseEventArgs e)
        {    
            dragging = true;
            WindowsManager.SelectClass(this);
        }

        public Control GetControl()
        {
            return panel;
        }

        public ClassData GetClassData()
        {
            return classData;
        }

        public Point GetCenter()
        {
            return new Point(panel.Location.X + panel.Width / 2, panel.Location.Y + panel.Height/2);
        }

        public int GetHeight()
        {
            return panel.Height;
        }

        public int GetWidth()
        {
            return panel.Width;
        }

        public void Select() 
        {
            labelName.BackColor = System.Drawing.Color.AliceBlue;
        }

        public void Deselect()
        {
            labelName.BackColor = System.Drawing.Color.Silver;
        }
    }
}
